package vn.com.vega.music.clientserver;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.com.vega.music.objects.Playlist;
import vn.com.vega.music.objects.Song;
import android.util.Log;

public class JsonSync extends JsonBase {

	public SyncDownstreamPackage aDownstreamPackage = new SyncDownstreamPackage();

	public static JsonSync doSync(SyncUpstreamPackage sup) {
		String url = ClientServer.getInstance().getUrlSync();
		JsonSync result = new JsonSync();
		JSONObject json = result.postAndParseBasicJson(url, sup.toJsonString());
		if (result.isSuccess()) {
			result.parseSyncDownstreamPackageJson(json);
		}
		url = null;
		json = null;
		return result;
	}

	protected void parseSyncDownstreamPackageJson(JSONObject json) {
		try {
			// Last version aka last update
			// aDownstreamPackage.iLastVersion = json.getInt("last_update");
			// aDownstreamPackage.iLastVersion = getInt(json, "last_update",
			// false);
			aDownstreamPackage.sLastVersion = getString(json, "last_update",
					false);
			JSONArray jsonArray;
			JSONObject jsonObject;

			// Updated playlists
			jsonArray = json.getJSONArray("updated");
			for (int i = 0; i < jsonArray.length(); i++) {
				jsonObject = jsonArray.getJSONObject(i);
				SyncDownstreamPlaylist sdp = parseSyncDownstreamPlaylist(jsonObject);
				aDownstreamPackage.aUpdatedPlaylists.add(sdp);
			}

			// Added playlists
			jsonArray = json.getJSONArray("added");
			for (int i = 0; i < jsonArray.length(); i++) {
				jsonObject = jsonArray.getJSONObject(i);
				SyncDownstreamPlaylist sdp = parseSyncDownstreamPlaylist(jsonObject);
				aDownstreamPackage.aAddedPlaylists.add(sdp);
			}

			// Deleted playlists
			jsonArray = json.getJSONArray("deleted");
			for (int i = 0; i < jsonArray.length(); i++) {
				aDownstreamPackage.aDeletedPlaylistIds.add(Integer
						.valueOf(jsonArray.getInt(i)));
			}

			// Register Ids
			jsonArray = json.getJSONArray("regids");
			for (int i = 0; i < jsonArray.length(); i++) {
				SyncIdRegisterResult sirr = parseSyncIdRegisterResult(jsonArray
						.getJSONObject(i));
				aDownstreamPackage.aIdRegisterResults.add(sirr);
			}

			// New songs
			jsonArray = json.getJSONArray("sync_song");
			for (int i = 0; i < jsonArray.length(); i++) {
				Song song = parseSong(jsonArray.getJSONObject(i));
				aDownstreamPackage.aNewSongs.add(song);
			}

			// Favourite playlist
			jsonArray = json.getJSONArray("favourite_playlist");
			for (int i = 0; i < jsonArray.length(); i++) {
				Playlist playlist = parseFavPlaylist(jsonArray.getJSONObject(i));
				aDownstreamPackage.aFavPlaylist.add(playlist);
				// aDownstreamPackage.aNewSongs.add(song);
			}

			jsonArray = null;
			jsonObject = null;

			// Follow/Unfollw artist

		} catch (Throwable t) {
			setParsingError();
		}
	}

	protected Playlist parseFavPlaylist(JSONObject json) {
		try {
			Playlist pl = new Playlist();
			pl.type = Playlist.TYPE_FAVOURITE_EX;
			pl.serverId = json.getInt("id");
			pl.title = getString(json, "name", false);
			pl.total_song = getInt(json, "total_song", false);
			pl.userThumb = getString(json, "creator_avatar_url", false);
			if (pl.title.equals("") || pl.title.equalsIgnoreCase("null"))
				pl.title = "Unknown";
			return pl;
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("parsePlaylist", e.getMessage());
		}
		return null;
	}

	protected SyncDownstreamPlaylist parseSyncDownstreamPlaylist(JSONObject json) {
		SyncDownstreamPlaylist playlist = new SyncDownstreamPlaylist();
		try {
			playlist.iServerId = json.getInt("id");
			playlist.sName = json.getString("name");
			playlist.iType = json.getInt("type");
			JSONArray songList = json.getJSONArray("song_list");
			for (int i = 0; i < songList.length(); i++) {
				playlist.aSongListIds.add(Integer.valueOf(songList.getInt(i)));
			}
		} catch (Throwable t) {
			setParsingError();
		}

		return playlist;
	}

	protected SyncIdRegisterResult parseSyncIdRegisterResult(JSONObject json) {
		SyncIdRegisterResult sirr = new SyncIdRegisterResult();
		sirr.iSubmitId = requireInt(json, "submitted");
		sirr.iRegisteredId = requireInt(json, "registered");
		return sirr;
	}
}
